package com.kaiwukj.android.ufamily.mvp.ui.page.home.service;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.mcas.utils.UToast;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.app.constant.Constant;
import com.kaiwukj.android.ufamily.mvp.http.entity.helper.ServiceHistoryEntry;
import com.kaiwukj.android.ufamily.mvp.http.entity.multi.ServiceItemMultiEntity;
import com.kaiwukj.android.ufamily.mvp.ui.page.service.community.CommunityServiceWebActivity;
import com.kaiwukj.android.ufamily.mvp.ui.widget.GridSpaceItemDecoration;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.kaiwukj.android.ufamily.utils.ServiceHistoryStore;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButtonDrawable;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundLinearLayout;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundRelativeLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class ServiceContentAdapter extends BaseMultiItemQuickAdapter<ServiceItemMultiEntity, BaseViewHolder> {

    /*  public ServiceContentAdapter() {
          super(R.layout.item_service_property_parent, new ArrayList<>());
      }

  */

    private ServiceHistoryAdapter historyAdapter;

    public ServiceContentAdapter(List<ServiceItemMultiEntity> data) {
        super(data);
        addItemType(ServiceItemMultiEntity.ITEM_HISTORY, R.layout.item_service_history_parent);
        addItemType(ServiceItemMultiEntity.ITEM_PROPERTY, R.layout.item_service_property_parent);
        addItemType(ServiceItemMultiEntity.ITEM_GM, R.layout.item_service_gm_parent);
        addItemType(ServiceItemMultiEntity.ITEM_COMMUNITY_SERVICE, R.layout.item_service_normal_parent);
        addItemType(ServiceItemMultiEntity.ITEM_SERVICE, R.layout.item_service_normal_parent);
    }

    public ServiceContentAdapter() {
        this(new ArrayList<>());
    }

    public void updateHistory(List<ServiceHistoryEntry> data) {

        if (historyAdapter != null) {
            historyAdapter.setList(data);
            for (ServiceItemMultiEntity entity : getData()) {
                if (entity.getItemType() == ServiceItemMultiEntity.ITEM_HISTORY) {
                    entity.setHistoryEntry(data);
                }
            }
        }
    }

    public boolean hasHistory() {
        for (ServiceItemMultiEntity entity : getData()) {
            if (entity.getItemType() == ServiceItemMultiEntity.ITEM_HISTORY) {
                return true;
            }
        }
        return false;
    }

    public boolean hasProperty() {
        for (ServiceItemMultiEntity entity : getData()) {
            if (entity.getItemType() == ServiceItemMultiEntity.ITEM_PROPERTY) {
                return true;
            }
        }
        return false;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, ServiceItemMultiEntity serviceItemMultiEntity) {
        switch (serviceItemMultiEntity.getItemType()) {
            case ServiceItemMultiEntity.ITEM_HISTORY:
                holder.setText(R.id.tv_title, "最近浏览");
                RecyclerView historyList = holder.getView(R.id.rv_service_item);
                historyList.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
                historyAdapter = new ServiceHistoryAdapter();
                if (historyList.getItemDecorationCount() == 0) {
                    historyList.addItemDecoration(new SpaceItemDecoration(6));
                }
                historyList.setAdapter(historyAdapter);
                historyAdapter.setList(serviceItemMultiEntity.getHistoryEntry());
                historyAdapter.setOnItemChildClickListener(((adapter, view, position) -> {
                    ServiceHistoryEntry item = historyAdapter.getItem(position);
                    //ServiceHistoryStore.put(item);

                    if (item.getType() == ServiceHistoryEntry.SERVICE_PROPERTY) {
                        propertyClick(item.getId());
                    } else if (item.getType() == ServiceHistoryEntry.SERVICE_GM) {
                        gmClick(item.getId());
                    } else {
                        intent2Detail(item.getId(), item.getLabel());
                    }

                }));
                historyAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
                    @Override
                    public void onChanged() {
                        super.onChanged();
                        LogUtils.d("onDataChanged---------------------->" + GsonUtils.toJson(historyAdapter.getData()));
                    }
                });
                break;
            case ServiceItemMultiEntity.ITEM_PROPERTY:
                holder.setText(R.id.tv_title, serviceItemMultiEntity.getServiceResult().getLabel());
                holder.setImageResource(R.id.iv_banner, serviceItemMultiEntity.getServiceResult().getId());
                RecyclerView rvList = holder.getView(R.id.rv_service_item);
                rvList.setLayoutManager(new GridLayoutManager(getContext(), 3));
                ServiceItemAdapter propertyAdapter = new ServiceItemAdapter();
                rvList.setAdapter(propertyAdapter);
                if (rvList.getItemDecorationCount() == 0) {
                    rvList.addItemDecoration(new GridSpaceItemDecoration(10, 10));
                }
                propertyAdapter.setNewData(serviceItemMultiEntity.getServiceResult().getChildren());
                propertyAdapter.setServiceType(1);
                propertyAdapter.setOnItemClickListener((adapter, view, position) -> {
                    propertyClick(propertyAdapter.getItem(position).getParentId());
                });
                break;
            case ServiceItemMultiEntity.ITEM_GM:
                float radius = SizeUtils.dp2px(10);
                float[] planA = {radius, radius, 0, 0, 0, 0, radius, radius};
                float[] planB = {0, 0, 0, 0, 0, 0, radius, radius};
                float[] planC = {radius, radius, 0, 0, 0, 0, 0, 0};
                QMUIRoundLinearLayout cardLayout = holder.getView(R.id.container_card);
                QMUIRoundRelativeLayout gmHeader = holder.getView(R.id.container_gm_header);
                QMUIRoundButtonDrawable cardDrawable = (QMUIRoundButtonDrawable) cardLayout.getBackground();
                QMUIRoundButtonDrawable gmDrawable = (QMUIRoundButtonDrawable) gmHeader.getBackground();

                if (hasProperty()) {
                    cardDrawable.setCornerRadii(planA);
                    gmDrawable.setCornerRadii(planC);
                } else {
                    cardDrawable.setCornerRadii(planB);
                    gmDrawable.setCornerRadius(0);
                }
                cardDrawable.invalidateSelf();
                gmDrawable.invalidateSelf();
                holder.setImageResource(R.id.iv_banner, serviceItemMultiEntity.getServiceResult().getId());
                RecyclerView gmList = holder.getView(R.id.rv_service_item);
                gmList.setLayoutManager(new GridLayoutManager(getContext(), 3));
                ServiceItemAdapter gmAdapter = new ServiceItemAdapter();
                gmList.setAdapter(gmAdapter);
                if (gmList.getItemDecorationCount() == 0) {
                    gmList.addItemDecoration(new GridSpaceItemDecoration(10, 10));
                }
                gmAdapter.setNewData(serviceItemMultiEntity.getServiceResult().getChildren());
                gmAdapter.setServiceType(2);
                gmAdapter.setOnItemClickListener(((adapter, view, position) -> {
                    gmClick(gmAdapter.getItem(position).getParentId());
                }));
                break;
            case ServiceItemMultiEntity.ITEM_COMMUNITY_SERVICE:
                holder.setText(R.id.tv_title, serviceItemMultiEntity.getServiceResult().getLabel());
                RecyclerView communityList = holder.getView(R.id.rv_service_item);
                communityList.setLayoutManager(new GridLayoutManager(getContext(), 3));
                ServiceItemAdapter communityAdapter = new ServiceItemAdapter();
                communityAdapter.setServiceType(1);
                communityList.setAdapter(communityAdapter);
                if (communityList.getItemDecorationCount() == 0) {
                    communityList.addItemDecoration(new GridSpaceItemDecoration(10, 10));
                }
                communityAdapter.setNewData(serviceItemMultiEntity.getServiceResult().getChildren());
                communityAdapter.setOnItemClickListener(((adapter, view, position) -> {
                    communityServiceClick(communityAdapter.getItem(position).getParentId());
                }));
                break;
            case ServiceItemMultiEntity.ITEM_SERVICE:
                holder.setText(R.id.tv_title, serviceItemMultiEntity.getServiceResult().getLabel());
                RecyclerView normalList = holder.getView(R.id.rv_service_item);
                normalList.setLayoutManager(new GridLayoutManager(getContext(), 3));
                ServiceItemAdapter normalAdapter = new ServiceItemAdapter();

                normalList.setAdapter(normalAdapter);
                if (normalList.getItemDecorationCount() == 0) {
                    normalList.addItemDecoration(new GridSpaceItemDecoration(10, 10));
                }
                normalAdapter.setNewData(serviceItemMultiEntity.getServiceResult().getChildren());
                normalAdapter.setOnItemClickListener(((adapter, view, position) -> {
                    if (normalAdapter.getItem(position).getProviderNum() > 0) {
                        intent2Detail(normalAdapter.getItem(position).getId(), normalAdapter.getItem(position).getLabel());
                    } else {
                        UToast.showShort(getContext(), "该小区暂未开通此服务");
                    }
                }));
                break;
        }
    }

    private void propertyClick(int id) {
        switch (id) {
            case ServiceHistoryEntry.PROPERTY_DOOR://开门
                ServiceHistoryStore.put(
                        new ServiceHistoryEntry(
                                ServiceHistoryEntry.SERVICE_PROPERTY,
                                ServiceHistoryEntry.PROPERTY_DOOR,
                                "开门"));
                //UToast.showShort(getContext(), "开门");
                ARouter.getInstance().build(ARouterUrlKt.MineDoorAccessUrl).navigation();
                break;
            case ServiceHistoryEntry.PROPERTY_REPORT://报修
                ServiceHistoryStore.put(
                        new ServiceHistoryEntry(
                                ServiceHistoryEntry.SERVICE_PROPERTY,
                                ServiceHistoryEntry.PROPERTY_REPORT,
                                "报修"));
                ARouter.getInstance().build(ARouterUrlKt.RepairsRouterUrl).navigation();
                break;
            case ServiceHistoryEntry.PROPERTY_FEE://缴费
                ServiceHistoryStore.put(
                        new ServiceHistoryEntry(
                                ServiceHistoryEntry.SERVICE_PROPERTY,
                                ServiceHistoryEntry.PROPERTY_FEE,
                                "缴费"));
                ARouter.getInstance().build(ARouterUrlKt.PayFeeUrl)
                        .navigation();
                break;
            case ServiceHistoryEntry.PROPERTY_PARK://停车
                ServiceHistoryStore.put(
                        new ServiceHistoryEntry(
                                ServiceHistoryEntry.SERVICE_PROPERTY,
                                ServiceHistoryEntry.PROPERTY_PARK,
                                "停车"));
                UToast.showShort(getContext(), "停车");
                break;
            case ServiceHistoryEntry.PROPERTY_TOUSU://投诉
                ServiceHistoryStore.put(
                        new ServiceHistoryEntry(
                                ServiceHistoryEntry.SERVICE_PROPERTY,
                                ServiceHistoryEntry.PROPERTY_TOUSU,
                                "投诉"));
                ARouter.getInstance().build(ARouterUrlKt.ComplainRouterUrl).navigation();
                break;
            case ServiceHistoryEntry.PROPERTY_NOTICE://物业公告
                ServiceHistoryStore.put(
                        new ServiceHistoryEntry(
                                ServiceHistoryEntry.SERVICE_PROPERTY,
                                ServiceHistoryEntry.PROPERTY_NOTICE,
                                "物业公告"));
                ARouter.getInstance().build(ARouterUrlKt.CommunityNoticeUrl)
                        .withString("EXTRA_KEY_FRAGMENT", "EXTRA_KEY_LIST")
                        .navigation();
                break;
        }
    }

    private void gmClick(int id) {
        switch (id) {
            case ServiceHistoryEntry.GM_MFSJ://免费设计
                ServiceHistoryStore.put(
                        new ServiceHistoryEntry(
                                ServiceHistoryEntry.SERVICE_GM,
                                ServiceHistoryEntry.GM_MFSJ,
                                "免费设计"));
                ARouter.getInstance().build(ARouterUrlKt.ServiceDetailUrl)
                        .withInt("serviceType", 4).navigation();
                break;
            case ServiceHistoryEntry.GM_XFZX://新房装修
                ServiceHistoryStore.put(
                        new ServiceHistoryEntry(
                                ServiceHistoryEntry.SERVICE_GM,
                                ServiceHistoryEntry.GM_XFZX,
                                "新房装修"));
                ARouter.getInstance().build(ARouterUrlKt.ServiceDetailUrl)
                        .withInt("serviceType", 1).navigation();
                break;
            case ServiceHistoryEntry.GM_JFGZ://旧房改造
                ServiceHistoryStore.put(
                        new ServiceHistoryEntry(
                                ServiceHistoryEntry.SERVICE_GM,
                                ServiceHistoryEntry.GM_JFGZ,
                                "旧房改造"));
                ARouter.getInstance().build(ARouterUrlKt.ServiceDetailUrl)
                        .withInt("serviceType", 2).navigation();
                break;
            case ServiceHistoryEntry.GM_MFBJ://免费报价
                ServiceHistoryStore.put(
                        new ServiceHistoryEntry(
                                ServiceHistoryEntry.SERVICE_GM,
                                ServiceHistoryEntry.GM_MFBJ,
                                "免费报价"));
                ARouter.getInstance().build(ARouterUrlKt.ServiceDetailUrl)
                        .withInt("serviceType", 3).navigation();
                break;
            case ServiceHistoryEntry.GM_ZXZB://装修直播
                ServiceHistoryStore.put(
                        new ServiceHistoryEntry(
                                ServiceHistoryEntry.SERVICE_GM,
                                ServiceHistoryEntry.GM_ZXZB,
                                "装修直播"));
                ARouter.getInstance().build(ARouterUrlKt.OrderGMWorkingListUrl)
                        .withInt("type", 1)
                        .navigation();
                break;
        }
    }

    private void communityServiceClick(int flag) {
        switch (flag) {
            case ServiceHistoryEntry.COMMUNITY_NEWS:
                ARouter.getInstance().build(ARouterUrlKt.CommunityNewsUrl).navigation();
                break;
            case ServiceHistoryEntry.COMMUNITY_ZYZM:
                ARouter.getInstance().build(ARouterUrlKt.CommunityServiceWeb)
                        .withInt("type", CommunityServiceWebActivity.ZYZM)
                        .withString("url", Constant.SERVICE_COMMUNITY_ZYZM)
                        .navigation();
                break;
            case ServiceHistoryEntry.COMMUNITY_EVENT:
                //http://committeeh5test.jiayuanli.net/guideList.html
                ARouter.getInstance().build(ARouterUrlKt.CommunityServiceWeb)
                        .withInt("type", CommunityServiceWebActivity.GUIDE)
                        .withString("url", Constant.SERVICE_COMMUNITY_GUIDE)
                        .navigation();
                break;
        }
    }

    private void intent2Detail(int serviceId, String serviceLabel) {
        ServiceHistoryStore.put(
                new ServiceHistoryEntry(
                        ServiceHistoryEntry.SERVICE_NORMAL,
                        serviceId,
                        serviceLabel));
        ARouter.getInstance().build(ARouterUrlKt.ServiceDetailUrl)
                .withSerializable("serviceId", serviceId)
                .navigation();
    }

}
